package cn.com.infohold.smartcity.activity.Dispute;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.api.HttpEvent;
import cn.com.infohold.smartcity.api.SDK;
import cn.com.infohold.smartcity.api.bean.DisputeBean;
import cn.com.infohold.smartcity.api.bean.LandResult;
import cn.com.infohold.smartcity.data.XmlData;
import cn.com.infohold.smartcity.parent.ParentActivity;
import cn.com.infohold.smartcity.utils.GsonUtil;
import cn.com.infohold.smartcity.utils.RequestCode;
import common.annotation.ViewInject;
import common.dialog.ProgressDialog;

/**
 * Created by Monsoon on 2017/11/8.
 */
public class DisputeDetail_Activity extends ParentActivity{

    @ViewInject(R.id.ib_back)
    ImageButton ib_back;
    @ViewInject(R.id.tv_disputeContent)
    TextView tv_disputeContent;
    @ViewInject(R.id.tv_disputeStatus)
    TextView tv_disputeStatus;
    @ViewInject(R.id.et_disputeStatus)
    EditText et_disputeStatus;
    @ViewInject(R.id.btn_commit)
    Button btn_commit;

    private DisputeBean disputeBean;
    private boolean isDispute;

    @Override
    protected Object inflateView() {
        return R.layout.activity_dispute_detail;
    }

    @Override
    protected void initView(View anchor) {

        Intent intent = getIntent();
        Bundle dispute_detail = intent.getBundleExtra("dispute_detail");
        if (dispute_detail != null){
            disputeBean = (DisputeBean) dispute_detail.getSerializable("dispute_detail");
            isDispute = dispute_detail.getBoolean("isDispute");

            if (isDispute){
                tv_disputeStatus.setVisibility(View.VISIBLE);
                tv_disputeContent.setVisibility(View.VISIBLE);
                et_disputeStatus.setVisibility(View.GONE);
                btn_commit.setVisibility(View.GONE);

                tv_disputeContent.setText(disputeBean.getDisputeContent());
                switch (disputeBean.getDisputeStatus()){
                    case 1:
                        tv_disputeStatus.setText("村级处理");
                        break;
                    case 2:
                        tv_disputeStatus.setText("乡级待处理");
                        break;
                    case 3:
                        tv_disputeStatus.setText("乡级处理");
                        break;
                    case 4:
                        tv_disputeStatus.setText("市级待处理");
                        break;
                    case 5:
                        tv_disputeStatus.setText("市级处理");
                        break;
                    case 9:
                        tv_disputeStatus.setText("删除");
                        break;
                    default:
                        break;
                }
            }else {
                et_disputeStatus.setVisibility(View.VISIBLE);
                tv_disputeContent.setVisibility(View.GONE);
                tv_disputeStatus.setVisibility(View.GONE);
                btn_commit.setVisibility(View.VISIBLE);
            }

        }

        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et_disputeStatus.getText().toString())){
                    Toast.makeText(DisputeDetail_Activity.this, "请输入纠纷状态", Toast.LENGTH_SHORT).show();
                    return;
                }
//                disputeBean.setDisputeStatus(et_disputeStatus.getText().toString());
                switch (disputeBean.getDisputeStatus()){
                    case 1:
//                        disputeBean.setDisputeStatus();
                        break;
                    case 2:
                        break;

                    default:
                        break;

                }

                initData();

            }
        });

    }

    private void initData(){
        SDK.api().addDispute(disputeBean).postEvent(RequestCode.REQUEST_CODE_EDIT_DISPUTE);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(HttpEvent<String> httpEvent) {

        int requestCode = httpEvent.getRequestCode();
        if (requestCode != RequestCode.REQUEST_CODE_EDIT_DISPUTE  ) {
            ProgressDialog.dismiss();
            return;
        }

        if (httpEvent.getState() != HttpEvent.STATE_SUCCESS) {
            ProgressDialog.dismiss();
            return;
        } else {
            switch (requestCode) {
                case RequestCode.REQUEST_CODE_EDIT_DISPUTE:
                    Type type = new TypeToken<LandResult<Map<String,String>>>(){}.getType();
                    LandResult<Map<String,String>> result = GsonUtil.fromJson(httpEvent.getData(),type);
                    Toast.makeText(this, result.getMessage(), Toast.LENGTH_SHORT).show();
                    if (result.getStatus().equals("1")){
                        finish();
                        overridePendingTransitionFade();
                    }

                    break;

                default:
                    break;
            }
        }
    }

}
